package com.cloud.sample.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Slf4j
@RefreshScope
@Configuration
public class WechatConfig {
    /**
     * 微信公众号的appid
     */
    @Value("${wechat.mpAppId}")
    private String mpAppId;

    /**
     * 微信公众号的secret
     */
    @Value("${wechat.mpAppSecret}")
    private String mpAppSecret;

    /**
     * 微信小程序的appid
     */
    @Value("${wechat.appId}")
    private String appId;

    /**
     * 微信小程序的secret
     */
    @Value("${wechat.appSecret}")
    private String appSecret;

    @Bean
    public WxMpService wxMpService() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        configStorage.setAppId(mpAppId);
        configStorage.setSecret(mpAppSecret);

        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(configStorage);
        return wxMpService;
    }
}
